package model

import (
	"fmt"
	"strings"
)

type MetricValue struct {
	Endpoint  string      `json:"endpoint"`    //主机名
	Metric    string      `json:"metric"`      //监控项名称
	Value     interface{} `json:"value"`       //监控值，支持float64 int64, string会转为float64
	Step      int64       `json:"step"`        //监控间隔
	Type      string      `json:"counterType"` //监控类型 GAUGE:单时间点采集 COUNTER:历史时间点差采集
	Tags      string      `json:"tags"`        //tag根据业务自定义
	Timestamp int64       `json:"timestamp"`   //时间戳
}

func (this *MetricValue) String() string {
	return fmt.Sprintf(
		"<Endpoint:%s, Metric:%s, Type:%s, Tags:%s, Step:%d, Time:%d, Value:%v>",
		this.Endpoint,
		this.Metric,
		this.Type,
		this.Tags,
		this.Step,
		this.Timestamp,
		this.Value,
	)
}

type FuncsAndInterval struct {
	Fs       []func() []*MetricValue
	Interval int
}

func NewMetricValue(metric string, val interface{}, dataType string, tags ...string) *MetricValue {
	mv := MetricValue{
		Metric: metric,
		Value:  val,
		Type:   dataType,
	}

	size := len(tags)

	if size > 0 {
		mv.Tags = strings.Join(tags, ",")
	}

	return &mv
}

func GaugeValue(metric string, val interface{}, tags ...string) *MetricValue {
	return NewMetricValue(metric, val, "GAUGE", tags...)
}

func CounterValue(metric string, val interface{}, tags ...string) *MetricValue {
	return NewMetricValue(metric, val, "COUNTER", tags...)
}
